package com.sunday.boot3.user.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sunday.common.core.asserts.BusinessAssert;
import com.sunday.common.core.converter.MapperConvert;
import com.sunday.common.core.enums.ErrorCodeEnum;
import com.sunday.common.core.remoting.base.page.PageResultDTO;
import com.sunday.common.mybatis.plus.query.PageWrapper;
import com.sunday.common.leaf.core.IdWorker;
import com.sunday.boot3.user.center.pojo.dto.user.role.RemoveUserRoleReqDTO;
import com.sunday.boot3.user.center.pojo.dto.user.role.SaveUserRoleReqDTO;
import com.sunday.boot3.user.center.pojo.dto.user.role.PageUserRoleResDTO;
import com.sunday.boot3.user.center.pojo.dto.user.role.UpdateUserRoleReqDTO;
import com.sunday.boot3.user.center.pojo.dto.user.role.PageUserRoleReqDTO;
import com.sunday.boot3.user.center.repository.dao.UserRoleDao;
import com.sunday.boot3.user.center.repository.entity.UserRole;
import com.sunday.boot3.user.center.service.UserRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Description: 用户角色关系表 服务实现类
 * @author: zsy
 * @Create: 2024/09/22 16:24:38
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserRoleServiceImpl implements UserRoleService {

    private final UserRoleDao userRoleDao;

    /**
     * @Description: 新增 用户角色关系表 数据
     * @Param: [saveUserRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void save(SaveUserRoleReqDTO saveUserRoleReqDTO) {
        UserRole userRole = MapperConvert.map(saveUserRoleReqDTO, UserRole.class);
        userRole.setId(IdWorker.getId());
        userRole.setCreateTime(LocalDateTime.now());
        BusinessAssert.isTrue(userRoleDao.save(userRole), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 删除 用户角色关系表 数据
     * @Param: [removeUserRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void remove(RemoveUserRoleReqDTO removeUserRoleReqDTO) {
        BusinessAssert.isTrue(userRoleDao.removeByIds(removeUserRoleReqDTO.getIds()), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 修改 用户角色关系表 数据
     * @Param: [updateUserRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public void update(UpdateUserRoleReqDTO updateUserRoleReqDTO) {
        LambdaUpdateWrapper<UserRole> userRoleUW = Wrappers.lambdaUpdate();
        userRoleUW.eq(UserRole::getId, updateUserRoleReqDTO.getId());
        userRoleUW.set(UserRole::getUserId, updateUserRoleReqDTO.getUserId())
                .set(UserRole::getRoleId, updateUserRoleReqDTO.getRoleId());
        BusinessAssert.isTrue(userRoleDao.update(userRoleUW), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 获取 用户角色关系表 分页列表数据
     * @Param: [pageUserRoleReqDTO]
     * @Return: com.sunday.common.core.remoting.base.page.PageResultDTO<com.sunday.boot3.user.center.pojo.dto.user.role.PageUserRoleResDTO>
     * @Author: zsy
     * @Time: 2024/09/22 16:24:38
     */
    @Override
    public PageResultDTO<PageUserRoleResDTO> page(PageUserRoleReqDTO pageUserRoleReqDTO) {
        LambdaQueryWrapper<UserRole> userRoleQW = Wrappers.lambdaQuery();
        userRoleQW.eq(ObjectUtils.isNotEmpty(pageUserRoleReqDTO.getId()), UserRole::getId, pageUserRoleReqDTO.getId())
                .eq(ObjectUtils.isNotEmpty(pageUserRoleReqDTO.getUserId()), UserRole::getUserId, pageUserRoleReqDTO.getUserId())
                .eq(ObjectUtils.isNotEmpty(pageUserRoleReqDTO.getRoleId()), UserRole::getRoleId, pageUserRoleReqDTO.getRoleId())
                .eq(ObjectUtils.isNotEmpty(pageUserRoleReqDTO.getCreateTime()), UserRole::getCreateTime, pageUserRoleReqDTO.getCreateTime());
        return PageWrapper.result(userRoleDao.page(PageWrapper.map(pageUserRoleReqDTO, UserRole::getId), userRoleQW), data -> MapperConvert.map(data, PageUserRoleResDTO.class));
    }

}
